home *** CD-ROM | disk | FTP | other *** search
/ PCMania 73 / PCMania CD73_1.iso / pcmania / demosc73 / GRAF.PAS < prev    next >
Pascal/Delphi Source File  |  1998-05-11  |  5KB  |  276 lines

  1. {$G+}
  2. unit graf;
  3.  
  4. INTERFACE
  5.  
  6. CONST
  7.      VGA :pointer=PTR($a000,$0);
  8.  
  9. TYPE
  10.     color  = record
  11.              r,g,b : byte;
  12.              end;
  13.     paleta = array [0..255] of color;
  14.  
  15. PROCEDURE Set_vga;
  16. PROCEDURE Set_text;
  17. PROCEDURE Cls(color:byte;VAR donde);
  18. PROCEDURE Flip(VAR desde,hasta);
  19. PROCEDURE PutRGB(Color,R,G,B : Byte);
  20. PROCEDURE GetRGB(Color:byte; VAR R, G, B:byte);
  21. PROCEDURE Border(col:BYTE);
  22. PROCEDURE Putpaleta(pal:paleta);
  23. PROCEDURE Getpaleta(VAR pal:paleta);
  24. PROCEDURE EsperaVGA;
  25. PROCEDURE Lee_pcx(Var origen; cont:word;VAR destino);
  26. PROCEDURE Load_pcx (origen:String;VAR destino);
  27. PROCEDURE Putpixel (X,Y : Integer; Col : Byte; where:word);
  28. PROCEDURE Line(x1, y1, x2, y2 : integer; color : byte; var donde);
  29.  
  30. IMPLEMENTATION
  31.  
  32. PROCEDURE Set_vga; ASSEMBLER;
  33. ASM
  34.    mov ax,13h
  35.    int 10h
  36. END;
  37.  
  38. PROCEDURE Set_text; ASSEMBLER;
  39. ASM
  40.    mov ax,3h
  41.    int $10
  42. END;
  43.  
  44. PROCEDURE Cls(color:byte;VAR donde);ASSEMBLER;
  45. ASM
  46.    les di,donde
  47.    mov al,byte ptr color
  48.    mov ah,al
  49.    db  $66, $c1, $e0, $10
  50.    mov al,byte ptr color
  51.    mov ah,al
  52.    mov cx,16000
  53.    db  $f3,$66,$ab
  54. end;
  55.  
  56. Procedure Flip (VAR desde,hasta); Assembler;
  57. ASM
  58.    mov  bx,ds
  59.    lds  si,desde
  60.    les  di,hasta
  61.    mov  cx,16000
  62.    db   $f3,$66,$a5    {rep movsd}
  63.    mov  ds,bx
  64. end;
  65.  
  66. PROCEDURE PutRGB(Color,R,G,B : Byte);
  67. BEGIN
  68.      Port[$3c8]:=Color;
  69.      Port[$3c9]:=R;
  70.      Port[$3c9]:=G;
  71.      Port[$3c9]:=B;
  72. END;
  73.  
  74. PROCEDURE GetRGB(Color:byte; VAR R, G, B:byte);
  75. BEGIN
  76.      Port[$3c7]:=Color;
  77.      R:=Port[$3c9];
  78.      G:=Port[$3c9];
  79.      b:=Port[$3c9];
  80. END;
  81.  
  82. PROCEDURE Border(col:BYTE); ASSEMBLER;
  83. ASM
  84.         mov ax,1001h
  85.         mov bh,col
  86.         int 10h
  87. END;
  88.  
  89. PROCEDURE PutPaleta (pal:paleta);
  90. VAR
  91.       cont : integer;
  92. BEGIN
  93.       Port[$3c8]:=0;
  94.       for cont:=0 to 255 do begin
  95.           Port[$3c9]:=pal[cont].r;
  96.           Port[$3c9]:=pal[cont].g;
  97.           Port[$3c9]:=pal[cont].b;
  98.       end;
  99. END;
  100.  
  101. PROCEDURE GetPaleta (VAR pal:paleta);
  102. VAR
  103.       cont : integer;
  104. BEGIN
  105.       for cont:=0 to 255 do
  106.       GetRGB(cont,pal[cont].r,pal[cont].g,pal[cont].b);
  107. END;
  108.  
  109. PROCEDURE EsperaVGA; assembler;
  110. asm
  111.     mov dx,3DAh
  112. @l1:
  113.     in al,dx
  114.     and al,08h
  115.     jnz @l1
  116. @l2:
  117.     in al,dx
  118.     and al,08h
  119.     jz  @l2
  120. end;
  121.  
  122.  
  123. PROCEDURE Putpixel (X,Y : Integer; Col : Byte; where :word);ASSEMBLER;
  124. ASM
  125.     mov      ax,where
  126.     mov      es,ax
  127.     mov      bx,[X]
  128.     mov      dx,[Y]
  129.     mov      di,bx
  130.     mov      bx, dx
  131.     shl      dx, 8
  132.     shl      bx, 6
  133.     add      dx, bx
  134.     add      di, dx
  135.     mov      al, [Col]
  136.     mov      es:[di],al
  137. END;
  138.  
  139. procedure Line(x1, y1, x2, y2 : integer; color : byte; var donde);
  140. var i, deltax, deltay, numpixels,
  141.     d, dinc1, dinc2,
  142.     x, xinc1, xinc2,
  143.     y, yinc1, yinc2 : integer;
  144.     sdonde,odonde   : word;
  145. begin
  146.  
  147.   sdonde:=seg(donde);
  148.   odonde:=ofs(donde);
  149.  
  150.   deltax := abs(x2 - x1);
  151.   deltay := abs(y2 - y1);
  152.   if deltax >= deltay then
  153.     begin
  154.       numpixels := deltax + 1;
  155.       d := (2 * deltay) - deltax;
  156.       dinc1 := deltay Shl 1;
  157.       dinc2 := (deltay - deltax) shl 1;
  158.       xinc1 := 1;
  159.       xinc2 := 1;
  160.       yinc1 := 0;
  161.       yinc2 := 1;
  162.     end
  163.   else
  164.     begin
  165.       numpixels := deltay + 1;
  166.       d := (2 * deltax) - deltay;
  167.       dinc1 := deltax Shl 1;
  168.       dinc2 := (deltax - deltay) shl 1;
  169.       xinc1 := 0;
  170.       xinc2 := 1;
  171.       yinc1 := 1;
  172.       yinc2 := 1;
  173.     end;
  174.  
  175.   { Make sure x and y move in the right directions }
  176.   if x1 > x2 then
  177.     begin
  178.       xinc1 := - xinc1;
  179.       xinc2 := - xinc2;
  180.     end;
  181.   if y1 > y2 then
  182.     begin
  183.       yinc1 := - yinc1;
  184.       yinc2 := - yinc2;
  185.     end;
  186.   for i := 1 to numpixels do
  187.     begin
  188.       MEM[sdonde:odonde+x1+y1*320]:=color;
  189.       if d < 0 then
  190.         begin
  191.           d  := d + dinc1;
  192.           x1 := x1 + xinc1;
  193.           y1 := y1 + yinc1;
  194.         end
  195.       else
  196.         begin
  197.           d  := d + dinc2;
  198.           x1 := x1 + xinc2;
  199.           y1 := y1 + yinc2;
  200.         end;
  201.     end;
  202. end;
  203.  
  204.  
  205.  
  206. Procedure Lee_pcx(Var origen; cont:word; VAR destino);ASSEMBLER;
  207. ASM
  208.    push  DS
  209.    sub   cont,768
  210.    lds   si, origen
  211.    add   si, cont
  212.    mov   cx, 256
  213.  
  214.    mov   dx, $3c8
  215.    mov   al, 0
  216.    out   dx, al
  217.    mov   dx, $3c9
  218.  
  219.  @@divide:
  220.    lodsb
  221.    shr   al, 2
  222.    out   dx, al
  223.    lodsb
  224.    shr   al, 2
  225.    out   dx, al
  226.    lodsb
  227.    shr   al, 2
  228.    out   dx, al
  229.    sub   cx, 1
  230.    jnz   @@divide
  231.  
  232.    les   di, destino
  233.    lds   si, origen
  234.    add   si, 128
  235.    mov   dx, cont
  236.    sub   dx, 1
  237.  
  238.  @@bucle:
  239.    lodsb
  240.    cmp   al, 192
  241.    Jb    @@norepite
  242.    sub   al, 192
  243.    mov   cl, al
  244.    lodsb
  245.    jmp   @@escribe
  246.  
  247.  @@norepite:
  248.    mov   cx, 1
  249.  
  250.  @@escribe:
  251.    rep   stosb
  252.    cmp   si, dx
  253.    jnz   @@bucle
  254.  
  255.  @@end:
  256.    pop ds
  257. END;
  258.  
  259. PROCEDURE Load_pcx (origen:String;VAR destino);
  260. VAR
  261.    pointr  : Pointer;
  262.    fichero : file;
  263.    tamany  : word;
  264. BEGIN
  265.      assign (fichero,origen);
  266.      reset (fichero,1);
  267.      getmem (pointr,filesize(fichero));
  268.      blockread (fichero,pointr^,filesize(fichero),tamany);
  269.      lee_pcx (pointr^,tamany,destino);
  270.      Freemem (pointr,filesize(fichero));
  271.      close (fichero);
  272. end;
  273.  
  274. begin
  275. end.
  276.